package cn.wolfcode.luowowo.article.controller;

import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTheme;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyService strategyService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyTagService strategyTagService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQueryObject qo){
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "strategyDetail/list";
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        //根据id删除记录
        strategyDetailService.deleteById(id);
        return new AjaxResult();
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state,Long id){
        //改变状态
        strategyDetailService.changeState(state,id);
        return new AjaxResult();
    }
    //转发到编辑页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        //strategies,共享所有的攻略到页面
        List<Strategy> strategies = strategyService.selectAllStrategy();
        model.addAttribute("strategies",strategies);
        //themes,共享所有的主题到页面
        List<StrategyTheme> themes = strategyThemeService.selectAllThemes();
        model.addAttribute("themes",themes);
        //判断id是否有值,决定是新增还是修改
        if(id == null){
            //新增
        }else{
            //修改
            //1.回显strategyDetail
            StrategyDetail strategyDetail = strategyDetailService.getById(id);
            model.addAttribute("strategyDetail",strategyDetail);
            //2.回显标签
            String tags = strategyTagService.getTags(id);
            model.addAttribute("tags",tags);
        }

        return "strategyDetail/input";
    }
    //根据攻略id获取攻略目录
    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(long strategyId){
        List<StrategyCatalog> list = strategyCatalogService.getByStrategyId(strategyId);
        return list;
    }
    //保存或者更新
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyDetail strategyDetail,String tags){
        //保存或者更新
        strategyDetailService.saveOrUpdate(strategyDetail,tags);

        return new AjaxResult();
    }

}
